var prefix = ctx + "system/dict";
var $,layer,table,form;
layui.use(['table', 'form', 'element', 'laydate', 'layer', 'jquery'], function () {
    var element = layui.element;
    form = layui.form;
    table = layui.table;
    $ = layui.$;
    layer = parent.layer === undefined ? layui.layer : top.layer;
    var laydate = layui.laydate;

    $(function () {
        laydate.render({
            elem: '#startTime',
            done: function (value, date) {
                console.log(value); //得到日期生成的值，如：2017-08-18
                console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
            }
        });
        //日期组件
        laydate.render({
            elem: '#endTime',
            done: function (value, date) {
                console.log(value); //得到日期生成的值，如：2017-08-18
                console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
            }
        });

        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export"
        };
        //初始化角色表
        initDictTypeTable();
    });


    /**
     * 初始化表格
     */
    function initDictTypeTable() {
        table.render({
            id: 'dictTypeList',
            elem: '#dictTypeList',
            method: 'POST',
            url: prefix + "/list",
            toolbar: '#toolbarDictType',
            title: '字典类型表',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {type: 'numbers', fixed: 'left', title: '序号'},
                {field: 'dictId', title: '字典主键', hide: true},
                {field: 'dictName', title: '字典名称'},
                {field: 'dictType', title: '字典类型',templet: function (row) {
                        return '<a href="javascript:void(0)" onclick="dictDetail(\'' + row.dictId + '\')">' + row.dictType + '</a>';
                    }
                },
                {
                    field: 'status', title: '角色状态', align: 'center', templet: function (row) {
                        return statusTools(row);
                    }
                },
                {field: 'remark', title: '备注'},
                {field: 'createTime', title: '创建时间'},
                {fixed: 'right', title: '操作', toolbar: '#barDictType'}
            ]],
            page: true,
            request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": '加载中。。。', //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.rows     //解析数据列表
                };
            }
        });
    }

    /**
     * 监听表头工具栏事件
     */
    table.on('toolbar(dictTypeList)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'addBtn':
                layer.open({
                    type: 1,
                    title:'添加',
                    content: '添加' //这里content是一个普通的String
                });
                break;
            case 'delBatchBtn':
                layer.msg(checkStatus.isAll ? '全选' : '未全选');
                break;
            case 'exportBtn':
                layer.open({
                    type: 1,
                    title:'导出',
                    content: '导出Excel' //这里content是一个普通的String
                });
                break;
        }
    });

    /**
     * 监听行工具事件
     */
    table.on('tool(dictTypeList)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 1,
                title:'编辑',
                content: '编辑' //这里content是一个普通的String
            });
        }else if (obj.event === 'dictDetail') {
            dictDetail(data.dictId);
        }
    });

    //搜索按钮
    form.on("submit(search)", function (data) {
        var whereData = data.field;
        table.reload('dictTypeList', {
            //设定异步数据接口的额外参数
            where: {
                "dictName": whereData.dictName,
                "dictType": whereData.dictType,
                "status": whereData.status,
                "params":{
                    "startTime": whereData.startTime,
                    "endTime": whereData.endTime
                }
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

    /* 角色状态显示 */
    function statusTools(row) {
        if (row.status == 1) {
            return '<span> <i class=\"layui-icon\">&#x1007;</i>停用</span>';
        } else {
            return '<span> <i class=\"layui-icon\">&#x1005;</i>启用</span>';
        }
    }

});

/*字典列表-详细*/
function dictDetail(dictId) {
    var url = prefix + '/detail/' + dictId;
    var index = layer.open({
        type: 2,
        title:'字典列表-详细',
        content: url,
        area: ['800px', '600px'],
        maxmin: true
    });
    layer.full(index);
}